package com.study_online.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.study_online.domain.*;
import com.study_online.mapper.QuestionMapper;
import com.study_online.service.QuestionService;

import com.study_online.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("questionService")
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;


    //通过面试题类型查找面试题目
    @Override
    public ResultInfo findSubjectByTid(int tid, Integer pageNum, Integer pageSize) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<QuestionSubject> list = questionMapper.findSubjectByTid(tid);
            PageInfo<QuestionSubject> pageInfo = new PageInfo<QuestionSubject>(list);
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败");
            e.printStackTrace();
        }
        return info;
    }


    //模糊查询面试题（题目）
    @Override
    public ResultInfo findSubjectName(String questionSName, Integer pageNum, Integer pageSize) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            PageHelper.startPage(pageNum, pageSize);
            String s = addPercentSign(questionSName);
            List<QuestionSubject> list = questionMapper.findSubjectName(s);
            PageInfo<QuestionSubject> pageInfo = new PageInfo<QuestionSubject>(list);
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败");
            e.printStackTrace();
        }
        return info;
    }

    //添加收藏
    @Override
    public ResultInfo addFavorite(int questionSid, int uid) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            QuestionFavorite questionFavorite = questionMapper.findFavoriteByUidAndSid(uid, questionSid);
            if (questionFavorite ==null){
                String addTime = TimeUtils.getTime();
                questionMapper.addFavorite(questionSid,uid,addTime);
            }else {
                info.setFlag(false);
                info.setErrorInfo("添加失败");
            }
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("添加失败");
            e.printStackTrace();
        }
        return info;
    }

    //通过收藏编号删除收藏的面试题目
    @Override
    public ResultInfo delFavoriteByFid(int questionFid) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            questionMapper.delFavoriteByFid(questionFid);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("删除失败");
            e.printStackTrace();
        }
        return info;
    }

    //通过用户ID查询用户收藏的面试题目
    @Override
    public ResultInfo findFavoriteByUid(int uid,int pageNum,int pageSize) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<QuestionFavorite> list = questionMapper.findFavoriteByUid(uid);
            PageInfo<QuestionFavorite> pageInfo = new PageInfo<QuestionFavorite>(list);
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败");
            e.printStackTrace();
        }
        return info;
    }

    //通过用户ID和题目ID查询用户收藏的面试题目
    @Override
    public ResultInfo findFavoriteByUidAndSid(int uid, int questionSid) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            QuestionFavorite questionFavorite = questionMapper.findFavoriteByUidAndSid(uid,questionSid);
            info.setData(questionFavorite);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败");
            e.printStackTrace();
        }
        return info;
    }

    //用户查看自己的错题库
    @Override
    public ResultInfo findWrongByUid(int uid,int pageNum,int pageSize) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<QuestionWrong> list = questionMapper.findWrongByUid(uid);
            PageInfo<QuestionWrong> pageInfo = new PageInfo<QuestionWrong>(list);
            info.setData(list);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("查看失败");
            e.printStackTrace();
        }
        return info;
    }

    //通过错题编号删除错题
    @Override
    public ResultInfo delWrongByWid(int questionWid) {
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        try {
            questionMapper.delWrongByWid(questionWid);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorInfo("删除失败");
            e.printStackTrace();
        }
        return info;
    }

    //给模糊查询添加百分号
    private static String addPercentSign(String info) {
        StringBuilder stringBuilder = new StringBuilder();
        String[] split = info.split("");
        for (int i = 0; i < split.length - 1; i++) {
            stringBuilder.append(split[i] + "%");
        }
        stringBuilder.append(split[split.length - 1]);
        return stringBuilder.toString();

    }

    // 获取套题
    @Override
    public ResultInfo getQuestions(int sid) {
        List<QuestionPart> questions = questionMapper.getQuestions(sid);
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        info.setData(questions);
        return info;
    }
}
